package ija.view;

import ija.controller.ReplayerController;
import ija.controller.ReplayerController.ReplayState;
import ija.model.CheckersModel;
import ija.model.basis.Desk;
import ija.model.history.History;
import ija.model.history.HistoryMove;
import ija.model.types.FigureColor;
import ija.view.elements.Board;
import ija.view.elements.ReplayerButtons;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

/**
 * Testing class for GUI operations
 */

public class ReplayerMenuContent extends JPanel implements Observer {
    private ReplayerController controller;
    private static final long serialVersionUID = 2502811299955940356L;
    private Board pBoard;
    private ReplayState replayState;

    private ReplayerButtons rButtons;
    public JTextArea taMoveHistory;
    private JLabel lMessage;
    private JScrollPane spMoveHistory;
    private String startingFileName;
    private FigureColor topPlayerColor = null;
    private boolean historyLoaded = false;

    public void setHistoryLoaded(boolean historyLoaded) {
        this.historyLoaded = historyLoaded;
    }

    public FigureColor getTopPlayerColor() {
        return this.topPlayerColor;
    }

    public String getTime() {
        return this.rButtons.getTime();
    }

    public void setTime(String s) {
        this.rButtons.setTime(s);
    }

    public void selectLine(Integer i) {
        String[] lines;
        int begin = 0, end = 0;
        int index = 0;
        lines = this.taMoveHistory.getText().split("\n");

        for (String line : lines) {
            index++;
            end = line.length() + begin;
            if (index == i) {
                break;
            }

            begin = end + 1;
        }
        if (begin > end) {
            return;
        }

        this.taMoveHistory.requestFocusInWindow();
        this.taMoveHistory.setSelectionColor(Color.GREEN);
        this.taMoveHistory.setSelectedTextColor(Color.WHITE);
        this.taMoveHistory.select(begin, end);
    }

    public String getStopAt() {
        return this.rButtons.getStopAt();
    }

    public void setStopAt(String s) {
        this.rButtons.setStopAt(s);
    }

    public void setTopPlayerColor(FigureColor tpc) {
        this.topPlayerColor = tpc;
    }

    public ReplayerMenuContent(ReplayerController controller) {
        super(new BorderLayout());
        this.controller = controller;
        this.controller.setView(this);
        this.getController().getModel().addObserver(this);
        this.addWidgets();
        this.addListeners();
        CheckersModel model = this.getController().getModel();
        this.getController().createLoadListener().actionPerformed(null);
        /*
         * model.initInputXml("examples/a.xml"); model.initHistoryXml();
         * model.initPlayersXml();
         * this.controller.createStopListener().actionPerformed(null);
         */
    }

    private ReplayerController getController() {
        return this.controller;
    }

    public ReplayState getState() {
        return this.replayState;
    }

    public void setState(ReplayState rs) {
        this.replayState = rs;
        this.rButtons.setState(rs);
    }

    public void setFromDesk(Desk d) {
        this.pBoard.setFromDesk(d);
    }

    public void addListeners() {
        this.rButtons.addPlayListener(this.controller.createPlayListener());
        this.rButtons.addStopListener(this.controller.createStopListener());
        this.rButtons.addPauseListener(this.controller.createPauseListener());
        this.rButtons.addFirstListener(this.controller.createFirstListener());
        this.rButtons.addLastListener(this.controller.createLastListener());
        this.rButtons.addPreviousListener(this.controller.createPreviousListener());
        this.rButtons.addNextListener(this.controller.createNextListener());
    }

    public String getMoveHistory() {
        return this.taMoveHistory.getText();
    }

    private void addWidgets() {
        Border blackline = BorderFactory.createLineBorder(Color.lightGray);
        Dimension boardSize = new Dimension(350, 350);
        Dimension buttonsSize = new Dimension(670, 60);
        Dimension historySize = new Dimension(350, 130);

        this.pBoard = new Board(8, null);
        this.pBoard.setMaximumSize(boardSize);
        this.pBoard.setMinimumSize(boardSize);
        this.pBoard.setPreferredSize(boardSize);

        this.lMessage = new JLabel("", SwingConstants.CENTER);
        this.lMessage.setMinimumSize(new Dimension(180, 20));
        this.lMessage.setMaximumSize(new Dimension(180, 20));
        this.lMessage.setPreferredSize(new Dimension(180, 20));

        this.rButtons = new ReplayerButtons("Replayer");
        this.rButtons.setMaximumSize(buttonsSize);
        this.rButtons.setMinimumSize(buttonsSize);
        this.rButtons.setPreferredSize(buttonsSize);

        this.taMoveHistory = new JTextArea(5, 20);
        this.spMoveHistory = new JScrollPane(this.taMoveHistory);
        this.spMoveHistory.setBorder(BorderFactory.createTitledBorder(blackline, "Move history"));
        this.spMoveHistory.setMaximumSize(historySize);
        this.spMoveHistory.setMinimumSize(historySize);
        this.spMoveHistory.setPreferredSize(historySize);

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout
                .createParallelGroup(GroupLayout.Alignment.CENTER, true)
                .addComponent(this.pBoard, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.lMessage, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.rButtons, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.spMoveHistory, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE));
        layout.setVerticalGroup(layout
                .createSequentialGroup()
                .addComponent(this.pBoard, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.lMessage, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.rButtons, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE)
                .addComponent(this.spMoveHistory, GroupLayout.DEFAULT_SIZE,
                        GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE));

    }

    @Override
    public void update(Observable o, Object obj) {
        if (obj instanceof Desk) {
            Desk desk = (Desk) obj;
            this.pBoard.setFromDesk(desk);
        } else if (obj instanceof History) {
            if (!this.historyLoaded) {
                History history = (History) obj;
                this.taMoveHistory.setText("");
                Iterator<HistoryMove> hmI = history.iterator();
                while (hmI.hasNext()) {
                    HistoryMove hm = hmI.next();
                    String round = this.controller.getNotationReader().translateRoundBack(hm)
                            + "\n";
                    this.taMoveHistory.append(round);
                }
                this.historyLoaded = true;
            }
        } else if (obj instanceof File) {

        }

        if (obj instanceof String) {
            this.lMessage.setText((String) obj);
        }

    }
}
